//
//  LKVisitorLoginView.swift
//  新浪微博
//
//  Created by 胡不归是大虾 on 16/9/24.
//  Copyright © 2016年 胡不归是大虾. All rights reserved.
//

import UIKit
import SnapKit
@objc protocol  LKVisitorLoginViewDelegate: NSObjectProtocol {
    @objc optional func userWillRegister()
    @objc optional func userWillLogin()
}
class LKVisitorLoginView: UIView {
    
    weak var delegate : LKVisitorLoginViewDelegate?
    //重写构造方法,设置UI
    override init(frame: CGRect) {
        super.init(frame: frame)
        setUpMyUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //更新页面标签信息
    func updateVistorViewInfo(tipTitle:String,imageName:String?)
    {
        tipLabel.text = tipTitle
        if let name = imageName {
            circleView.image = UIImage(named: name)
            homeView.isHidden = true
            bringSubview(toFront: circleView)
        }
        else{
        startAnimation()
        }
    }
    //让圆环动起来
    func startAnimation()
    {
        let rotate = CABasicAnimation(keyPath: "transform.rotation")
        rotate.toValue = M_PI * 2
        rotate.repeatCount = MAXFLOAT
        rotate.duration = 15
        rotate.isRemovedOnCompletion = false
        circleView.layer.add(rotate, forKey: nil)
    }
    //创建UI
    func setUpMyUI ()
    {
        //设置背景色
        let colorValue : CGFloat = 237 / 255.0
        backgroundColor = UIColor(red: colorValue, green: colorValue, blue: colorValue, alpha: 1)
        //添加子控件
        addSubview(circleView)
        addSubview(backView)
        addSubview(homeView)
        addSubview(tipLabel)
        addSubview(loginBtn)
        addSubview(registerBtn)
        //设置约束
        circleView.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.centerY.equalToSuperview().offset(-80)
        }
        backView.snp.makeConstraints { (make) in
            make.center.equalTo(circleView)
        }
        homeView.snp.makeConstraints { (make) in
            make.center.equalTo(circleView)
        }
        tipLabel.snp.makeConstraints { (make) in
            make.centerX.equalTo(circleView)
            make.top.equalTo(circleView.snp.bottom).offset(16)
        }
        loginBtn.snp.makeConstraints { (make) in
            make.left.equalTo(tipLabel)
            make.top.equalTo(tipLabel.snp.bottom).offset(16)
            make.width.equalTo(100)
        }
        registerBtn.snp.makeConstraints { (make) in
            make.right.equalTo(tipLabel)
            make.top.equalTo(tipLabel.snp.bottom).offset(16)
            make.width.equalTo(100)
        }
        loginBtn .addTarget(self, action: #selector(loginBtndidClick), for: .touchUpInside)
        registerBtn.addTarget(self, action: #selector(registerBtndidClick), for: .touchUpInside)
    }
    //按钮点击方法响应,应该交由控制器实现,所以通过代理
    @objc private func loginBtndidClick()
    {
        delegate?.userWillLogin?()
    }
    @objc private func registerBtndidClick()
    {
        delegate?.userWillRegister?()
    }
    //懒加载子控件
    private lazy var circleView = UIImageView(image:#imageLiteral(resourceName: "visitordiscover_feed_image_smallicon"))
    private lazy var backView : UIImageView = UIImageView(image: #imageLiteral(resourceName: "visitordiscover_feed_mask_smallicon"))
    private lazy var homeView : UIImageView = UIImageView(image: #imageLiteral(resourceName: "visitordiscover_feed_image_house"))
    private lazy var tipLabel : UILabel = {
        let tipLabel = UILabel(title: "哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈", font: 14, textColor: UIColor.darkGray)
        tipLabel.numberOfLines = 0
        tipLabel.preferredMaxLayoutWidth = 224
        return tipLabel
    }()
    private lazy var loginBtn : UIButton = {
        let btn = UIButton(title: "登录", font: 14, color: UIColor.darkGray)
        btn.setBackgroundImage(#imageLiteral(resourceName: "common_button_white"), for: .normal)
        return btn
    }()
    private lazy var registerBtn : UIButton = {
        let btn = UIButton(title: "注册", font: 14, color: UIColor.orange)
        btn.setBackgroundImage(#imageLiteral(resourceName: "common_button_white"), for: .normal)
        return btn
    }()
}
